﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;

[TestClass]
public class OrderServiceTests
{
    [TestMethod]
    public void AddOrder_ValidOrder_ShouldAddToList()
    {
        var service = new OrderService();
        var order = new Order { Id = 1 };
        service.AddOrder(order);
        Assert.AreEqual(1, service.GetOrdersCount());
    }

    [TestMethod]
    [ExpectedException(typeof(InvalidOperationException))]
    public void AddOrder_DuplicateOrder_ShouldThrowException()
    {
        var service = new OrderService();
        var order = new Order { Id = 1 };
        service.AddOrder(order);
        service.AddOrder(order); // 重复添加应抛异常
    }

    [TestMethod]
    public void Export_ShouldSerializeOrdersToXml()
    {
        var service = new OrderService();
        var order1 = new Order { Id = 1 };
        var order2 = new Order { Id = 2 };
        service.AddOrder(order1);
        service.AddOrder(order2);

        string filePath = "orders.xml";
        service.Export(filePath);

        Assert.IsTrue(File.Exists(filePath));
    }

    [TestMethod]
    public void Import_ShouldDeserializeOrdersFromXml()
    {
        var service = new OrderService();
        var order1 = new Order { Id = 1 };
        var order2 = new Order { Id = 2 };
        service.AddOrder(order1);
        service.AddOrder(order2);

        string filePath = "orders.xml";
        service.Export(filePath);

        var newService = new OrderService();
        newService.Import(filePath);

        Assert.AreEqual(2, newService.GetOrdersCount());
    }
}